#!/bin/bash
file_name=functions.sh
funcs=`which $file_name`
if [ "$funcs" != "" ] ;then
  echo "funcs=$funcs"
  . $funcs
else
  funcs=`dirname $0`/$file_name
  echo "$PWD"
  echo "funcs=$funcs"
  if [ "$funcs" != "" ] ;then
    . $funcs
  else
    exit
  fi
fi

#产品线名称:packages-w1140 必须跟主线一致 http://192.168.0.123/repositories/AppsOfMtk/product/PD1110/packages-w1140/
#
moudules_file=/tmp/modules
moudules_url=/tmp/modules_url
products_url=/tmp/products_url
svn_error=/tmp/svn.error
config_file=$1
authz_file=$2
tags_list=`parseini.shc $config_file get reposinfo tags | tr ',' ' '`
packages_list=`parseini.shc $config_file get reposinfo packages | tr ',' ' '`
appstype_list=`parseini.shc $config_file get reposinfo appstype|tr ',' ' '`
projects_list=`parseini.shc $config_file get reposinfo projects | tr ',' ' '`
domain=`parseini.shc $config_file get reposinfo domain`

if test ! -f  $config_file; then
  echoTextRed "$0  config_file  auth_file"
  exit
fi

#获取仓库最新的一些信息
rm -rf ${moudules_file}*
rm -rf $svn_error
if [ -z "$authz_file" ]; then
  echoTextBlue "正在获取仓库的配置信息..... "
  repos_list=`parseini.shc $config_file get reposinfo repos_app | tr ',' ' '`
  for repo in $repos_list
  do #1
      echoTextBlue "repo=$repo"
      for package in $packages_list
      do #2
        #echo "package=$package"
        for tag in $tags_list
        do #3
	  if [ "$tag" = "product" ]; then
	    for project in $projects_list
	    do
	      result=`svn.shc list http://$domain/repositories/$repo/$tag/$project/$package/ 2>> $svn_error `
	      if [ -n "$result" ]; then
		echo "http://$domain/repositories/$repo/$tag/$project/$package/" >> ${products_url} 
	      fi
	    done
	  fi
          for appstype in $appstype_list
          do #4
	    modules_list1=`cat $config_file |grep "\[$appstype"|sort | uniq`
	    if [ "$tag" != "trunk" ]; then
	      continue
	    fi	
            modules_list2=`svn.shc list http://$domain/repositories/$repo/$tag/$package/$appstype  2>> $svn_error | grep / |tr -d '/' `	  
	    for module in $modules_list1
	    do
	      echo "$module" >> ${moudules_file}1
	    done
	    for module in $modules_list2
	    do
	      echo "[${appstype}_${module}]" >> ${moudules_file}2
	      echo "http://$domain/repositories/$repo/$tag/$package/$appstype/$module" >> ${moudules_url} 
	    done	  
          done #4
        done #3
      done  #2   
  done #1
  cat ${moudules_file}1 | sort | uniq>${moudules_file} 
  cat ${moudules_file} > ${moudules_file}1
  cat ${moudules_file}2 | sort | uniq>${moudules_file} 
  cat ${moudules_file} > ${moudules_file}2
  sh_filter-out "`cat ${moudules_file}1|tr '\n' ' '`","`cat ${moudules_file}2|tr '\n' ' '`" | tr ' ' '\n' |sort|uniq >${moudules_file} 

  repos_list=`parseini.shc $config_file get reposinfo repos_framework | tr ',' ' '`
  for repo in $repos_list
  do
    for tag in $tags_list
    do
      if [ "$tag" = "product" ]; then
	for project in $projects_list
	do
	  result=`svn.shc list http://$domain/repositories/$repo/$tag/$project 2>> $svn_error`
	  if [ -n "$result" ]; then
	    echo "http://$domain/repositories/$repo/$tag/$project" >> ${products_url} 
	  fi
	done
      fi
    done
  done 
  cat ${products_url} | sort | uniq>${products_url}1 
  cat ${products_url}1 > ${products_url}
  #显示生成的文件信息
  echoTextGreen "====================================="
  cat ${moudules_file}
  echoTextGreen "====================================="
  echoTextBlue "以上模块未配置权限,列表文件: ${moudules_file}"
  echoTextBlue "应用模块svn路径信息: ${moudules_url}"
  echoTextBlue "产品线路径信息: ${products_url}"
  exit
fi

######################################################
##以下代码生成权限文件
echoTextBlue "权限文件 $authz_file 生成中..... "
if test ! -f $authz_file; then
  touch $authz_file
else
  result=`question "权限文件 $authz_file 已经存在,是否覆盖?" 1 yes`
  if [ "$result" = "yes" ]; then
    mv $authz_file ${authz_file}_`sh_get_date_and_time2`
    touch $authz_file
  else
    exit
  fi
fi

echoTextBlue "生成默认组信息......"
date
start_time=`sh_get_date_and_time2`
groups_list=`cat $config_file |grep def_g_ |awk -F"=" '{print $1}'`
echo "[groups]" > $authz_file 
#groups_list=`parseini.shc $config_file get groups groups|tr ',' ' '`
for group in $groups_list
do
	user_list=`cat $config_file |grep ^$group|awk -F"=" '{print $2}'`
	echo "$group=$user_list"   >> $authz_file 
done

echoTextBlue "通过模块生成组信息......"
date
modules_list=
for appstype in $appstype_list
do
    ac_list=`cat $config_file |grep "\[$appstype"|sort | uniq |tr '[' ' '|tr ']' ' ' |tr '\n' ' '`
    modules_list="$modules_list $ac_list"

done
#去掉重复项wallpapers|gles20_Basic等
modules_list="`echo $modules_list |sed "s/ /\\n/g" | sort | uniq |tr '\n' ' '`"
for module in $modules_list
do
  result=`parseini.shc $config_file  get $module usersofpermit`
  if [ -n "$result" ]; then
    usersofpermit=`echo $result|tr ',' ' '`
    #临时变量赋空值
    ac_usersofpermit=
    for user in $usersofpermit
    do
      result=`echo "$user" |grep @`
      if [ -n "$result" ]; then
	result=`echo $result|tr -d '@'`
        ac_usersofpermit="$ac_usersofpermit,`parseini.shc $config_file  get groups $result`"
	if [ -n "$ac_usersofpermit" ]; then
	  ac_usersofpermit=`echo $ac_usersofpermit | sed 's/.$//'`
        fi
      else
	ac_usersofpermit="$ac_usersofpermit,$user"
	ac_usersofpermit=`echo $ac_usersofpermit | sed 's/^,//'`
      fi
    done
    echo "g_$module=$ac_usersofpermit" >>$authz_file
    #result=`parseini.shc $authz_file set groups g_$module $result`
  else
    echo "g_$module=$result" >>$authz_file
    #result=`parseini.shc $authz_file set groups g_$module " "`
  fi
done
echo "">>$authz_file

echoTextBlue "应用仓库权限生成......"
date
repos_list=`parseini.shc $config_file get reposinfo repos_app | tr ',' ' '`
for repo in $repos_list
do
  echo "[${repo}:/]">>$authz_file
  echo "@def_g_admins_w = rw" >>$authz_file  
  echo "@def_g_admins_r = r" >>$authz_file
  echo "@def_g_compiler = rw" >>$authz_file 
  echo "@def_g_softdev_depart2 = r" >>$authz_file 
  for tag in $tags_list
  do
    ###for package in $packages_list
    ###do
      if [ "$tag" = "product" ] || [ "$tag" = "special" ] || [ "$tag" = "branch" ]; then
        projects_lists=`svn.shc list http://$domain/repositories/$repo/$tag |tr -d '/'`
        if  [ "$projects_lists" != "" ];then
	for project in $projects_lists 
	do
	  #is_url=`svn.shc info http://$domain/repositories/$repo/$tag/$project  2>> $svn_error|grep URL`
	  #屏蔽以下几行会加快速度,但是会生成无用的配置信息
	  echo "svn.shc list http://$domain/repositories/$repo/$tag/$project"
	  result=`svn.shc list http://$domain/repositories/$repo/$tag/$project 2>> $svn_error`
          if [ -z "$result" ]; then
            continue
          fi
	  ac_packages=`echo "$result" |tr -d '/'`
	  for ac_package in $ac_packages
	  do
	    not_product=`echo $ac_package |grep "for-"`
	    for module in $modules_list
	    do
	      echo "[${repo}:/$tag/$project/$ac_package/$module]"|tr '|' '/'>>$authz_file
	      if [ "$not_product" = "" ];then
	          echo "@g_$module = r" >>$authz_file
	      else
	          echo "@g_$module = rw" >>$authz_file	 
	      fi   
	    done
	  done
	done
	fi
      else
	if [ "$tag" = "trunk" ]; then
	echo "svn.shc list http://$domain/repositories/$repo/$tag"
        result=`svn.shc list http://$domain/repositories/$repo/$tag 2>> $svn_error`
        if [ -z "$result" ]; then
          continue
        fi
	ac_packages=`echo "$result" |tr -d '/'`
	for ac_package in $ac_packages
	do
	  for module in $modules_list
	  do


	      echo "[${repo}:/$tag/$ac_package/$module]"|tr '|' '/'>>$authz_file
	      echo "@g_$module = rw" >>$authz_file	    	    

	  done 
	done
	fi
      fi
    ###done
  done
done
echo "">>$authz_file
end_time=`sh_get_date_and_time2`
((sun_time1=${end_time}-${start_time}))
echoTextBlue "耗时: $sun_time1 秒!"

echoTextBlue "框架仓库权限生成......"
date
start_time=`sh_get_date_and_time2`
repos_list=`parseini.shc $config_file get reposinfo repos_framework | tr ',' ' '`
for repo in $repos_list
do
  echo "[${repo}:/]">>$authz_file
  echo "@def_g_admins_w = rw" >>$authz_file
  echo "@def_g_admins_r = r" >>$authz_file
  echo "@def_g_engineers = r" >>$authz_file
  echo "@def_g_compiler = rw" >>$authz_file 
  echo "@def_g_softdev_depart2 = r" >>$authz_file 
  for tag in $tags_list
  do

    if [ "$tag" = "trunk" ] || [ "$tag" = "branch" ] || [ "$tag" = "special" ]; then
          echo "[${repo}:/$tag]">>$authz_file
          echo "@def_g_engineers = rw" >>$authz_file
    fi
    
    if [ "$tag" = "product" ]; then
      projects_lists=`svn.shc list http://$domain/repositories/$repo/$tag|tr -d '/'`
      if  [ "$projects_lists" != "" ];then
      for project in $projects_lists
      do
	echo "svn.shc list http://$domain/repositories/$repo/$tag/$project"
        frameworks_list=`svn.shc list http://$domain/repositories/$repo/$tag/$project 2>> $svn_error | grep /`

        if [ -z "$frameworks_list" ]; then
           continue
        fi
	#frameworks_list=`svn.shc list http://$domain/repositories/$repo/$tag 2>> $svn_error | grep /`
	for framework in $frameworks_list
	do
	  framework=`echo ${framework%/*}`
	  not_product=`echo $framework |grep "for"`
	  if [ "$not_product" != "" ];then
	      echo "[${repo}:/$tag/$project/$framework]">>$authz_file
	      echo "@def_g_engineers = rw" >>$authz_file
	  fi	
	 done	
      done
   # else
      #frameworks_list=`svn.shc list http://$domain/repositories/$repo/$tag  2>> $svn_error | grep /`
      #for framework in $frameworks_list
      #do
	#framework=`echo ${framework%/*}`

	#if [ "$tag" = "trunk" ] || [ "$tag" = "branch" ]; then
	  #echo "[${repo}:/$tag/$framework]">>$authz_file
	  #echo "@def_g_engineers = rw" >>$authz_file	  	  
	#fi
      #done
      fi
    fi
  done
done
end_time=`sh_get_date_and_time2`
((sun_time2=${end_time}-${start_time}))
echoTextBlue "耗时: $sun_time2 秒!"
((sun_time=${sun_time1}+${sun_time2}))
echoTextBlue "总耗时: $sun_time 秒!"
date
chmod 777 $authz_file
replace=`question "是否覆盖权限文件authz-sourcecode-auto?"  2 "yes"`
if [ "$replace" = "yes" ];then
	cp $authz_file  authz-sourcecode-auto
fi
#####################################################################################################3
exit
do_recursive3()
{
    local app_url=$1
    result=`svn.shc list $app_url  |grep $2 -w`
    if [ -n "$result" ]; then
      echo "$app_url/$result">&2
      echo "$app_url/$result"
      return
    fi
    for ac_dir in `svn.shc.shc list $app_url |grep /`
    do
      app_url=$app_url/$ac_dir
     # echo "aaa=$app_url"
      do_recursive3 $app_url $2
      app_url=`dirname $app_url`
    done
    return 0
}

url_list="
http://smartphone/repositories/AppsOfMtk/branch
http://smartphone/repositories/AppsOfMtk/product
http://smartphone/repositories/AppsOfMtk/release
http://smartphone/repositories/AppsOfMtk/special
http://smartphone/repositories/AppsOfMtk/tags
"
apps_list="
http://smartphone/repositories/MT6575/trunk
http://smartphone/repositories/MT6575/branch
http://smartphone/repositories/MT6575/trunk
"
apps_lists=
for u in $url_list
do
apps_list=`do_recursive3 $u  apps`
apps_lists="$apps_list $apps_lists"
done
echoTextBlue "分支:"
apps_lists=`echo "$apps_lists" | "s#//#/#g"`
echoTextGreen "apps_lists=$apps_lists"
exit
